﻿namespace jzgl.Windows
{
    using Excel;
    using jzgl.Controller;
    using jzgl.Model;
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Text;
    using System.Windows.Forms;
    using jzgl.Helper;

    public class Mater_Info : Form
    {
        private System.Windows.Forms.Button btn_DaoChu;
        private System.Windows.Forms.Button btn_DaoRu;
        private DataGridViewTextBoxColumn Column1;
        private DataGridViewTextBoxColumn Column2;
        private DataGridViewTextBoxColumn Column3;
        private DataGridViewTextBoxColumn Column4;
        private DataGridViewTextBoxColumn Column5;
        private DataGridViewTextBoxColumn Column6;
        private ComboBox comboBox1;
        private IContainer components = null;
        private DataGridView dataGridView1;
        private DataGridViewTextBoxColumn dataGridViewTextBoxColumn1;
        private DataGridViewTextBoxColumn dataGridViewTextBoxColumn2;
        private DataGridViewTextBoxColumn dataGridViewTextBoxColumn3;
        private DataGridViewTextBoxColumn dataGridViewTextBoxColumn4;
        private DataGridViewTextBoxColumn dataGridViewTextBoxColumn5;
        private DataGridViewTextBoxColumn dataGridViewTextBoxColumn6;
        private int info = 0;
        private MaterInfoDB infoDB = new MaterInfoDB();
        private System.Windows.Forms.Label label1;
        private MaterInfo mater = new MaterInfo();
        private ConnDB open = new ConnDB();
        private OpenFileDialog openFileDialog1;
        private Panel panel1;
        private System.Data.DataTable table = null;
        private MaterTypeDB typeDB = new MaterTypeDB();

        public Mater_Info()
        {
            this.InitializeComponent();
            this.dataGridView1.AllowUserToResizeRows = true;
            this.dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
            this.dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders;
            this.Data_bind();
            this.ComboBox_bind();
            this.label1.Text = string.Concat(new object[] { "目前共有", this.typeDB.Count_MaterType(), "个分类，", this.info, "条物料记录" });
        }

        private void btn_DaoChu_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.dataGridView1.Rows.Count == 0)
                {
                    MessageBox.Show("没有数据不能导出！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                else
                {
                    this.ExportDataGridview(this.dataGridView1, true);
                }
            }
            catch (Exception)
            {
            }
        }

        private void btn_DaoRu_Click(object sender, EventArgs e)
        {
            try
            {
                this.openFileDialog1.Filter = "Execl files (*.xls)|*.xls";
                this.openFileDialog1.FilterIndex = 0;
                this.openFileDialog1.RestoreDirectory = true;
                this.openFileDialog1.Title = "导入物料文件";
                if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    string fileName = this.openFileDialog1.FileName;
                    string str2 = fileName.Substring(fileName.LastIndexOf(".")).ToString().Trim();
                    string str3 = fileName.Substring(fileName.LastIndexOf(@"\")).ToString().Trim();
                    string filename = str3.Substring(1, str3.IndexOf(".") - 1);
                    if (str2 != ".xls")
                    {
                        MessageBox.Show("文件格式不正确！");
                    }
                    else
                    {
                        DataSet set = null;
                        set = ConnDB.ExcelToDS(this.openFileDialog1.FileName, filename);
                        if (set != null)
                        {
                            if (set.Tables[0].Rows.Count > 0)
                            {
                                System.Data.DataTable table = set.Tables[0];
                                if (table.Columns.Count >= 0)
                                {
                                    this.table = this.infoDB.Show_MaterInfo();
                                    int num3 = this.infoDB.Delete_MaterInfo();
                                    for (int i = 1; i < table.Rows.Count; i++)
                                    {
                                        this.mater._MaterInfoName = table.Rows[i][1].ToAnythingString();
                                        this.mater._MaterUnit = table.Rows[i][2].ToAnythingString();
                                        this.mater._MaterPrice = table.Rows[i][3].ToAnythingString();
                                        this.mater._MaterTech = table.Rows[i][4].ToAnythingString();
                                        this.mater._MaterType = this.typeDB.Show_MaterId_ByName(table.Rows[i][0].ToAnythingString());
                                        this.mater._MaterTypeName = table.Rows[i][0].ToString();
                                        int num5 = this.infoDB.Add_MaterInfo(this.mater);
                                    }
                                }
                            }
                            goto Label_02B6;
                        }
                        MessageBox.Show("导入失败，请检查：\n数据源是否为空 \n数据源格式是否正确 \n文件名是否修改");
                    }
                    return;
                }
            Label_02B6:
                this.Data_bind();
            }
            catch (Exception)
            {
            }
        }

        private void btn_GuanBi_Click(object sender, EventArgs e)
        {
            try
            {
                base.Dispose();
            }
            catch (Exception)
            {
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.Rows.Count == 0)
            {
                MessageBox.Show("没有数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            else if (this.dataGridView1.CurrentRow.Cells[5].Value.ToString().Trim().Equals(""))
            {
                MessageBox.Show("请选择要删除的行！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            else if (MessageBox.Show("确定要删除吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                int num = this.infoDB.Delete_MaterInfo(this.dataGridView1.CurrentRow.Cells[5].Value.ToString().Trim());
                this.Data_bind();
            }
        }

        public void ComboBox_bind()
        {
            try
            {
                this.table = this.typeDB.Show_MaterType();
                DataRow row = this.table.NewRow();
                row[0] = 0;
                row[1] = "所有类";
                this.table.Rows.InsertAt(row, 0);
                this.comboBox1.DataSource = this.table;
                this.comboBox1.DisplayMember = "MaterTypeName";
                this.comboBox1.ValueMember = "MaterTypeId";
            }
            catch (Exception)
            {
            }
        }

        private void comboBox1_Click(object sender, EventArgs e)
        {
            try
            {
            }
            catch (Exception)
            {
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (int.Parse(this.comboBox1.SelectedValue.ToString().Trim()) == 0)
                {
                    this.Data_bind();
                }
                else
                {
                    this.Data_bind(this.comboBox1.Text.ToString().Trim());
                }
            }
            catch (Exception)
            {
            }
        }

        public void Data_bind()
        {
            try
            {
                this.dataGridView1.Rows.Clear();
                this.table = this.infoDB.Show_MaterInfo();
                if (this.table != null)
                {
                    for (int i = 0; i < this.table.Rows.Count; i++)
                    {
                        object[] values = new object[] { this.table.Rows[i][7].ToString(), this.table.Rows[i][1].ToString(), this.table.Rows[i][2].ToString(), float.Parse(this.table.Rows[i][3].ToString()).ToString("f2"), this.table.Rows[i][4].ToString(), this.table.Rows[i][0].ToString() };
                        this.dataGridView1.Rows.Add(values);
                        if ((i % 2) == 0)
                        {
                            this.dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.LightGray;
                        }
                        this.info++;
                    }
                }
            }
            catch (Exception)
            {
            }
        }

        public void Data_bind(string type_name)
        {
            try
            {
                this.dataGridView1.Rows.Clear();
                this.table = this.infoDB.Show_MaterInfoBY_typeName(type_name);
                if (this.table != null)
                {
                    for (int i = 0; i < this.table.Rows.Count; i++)
                    {
                        object[] values = new object[] { this.table.Rows[i][7].ToString(), this.table.Rows[i][1].ToString(), this.table.Rows[i][2].ToString(), this.table.Rows[i][3].ToString(), this.table.Rows[i][4].ToString(), this.table.Rows[i][0].ToString() };
                        this.dataGridView1.Rows.Add(values);
                        if ((i % 2) == 0)
                        {
                            this.dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.LightGray;
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            try
            {
                System.Drawing.Rectangle bounds = new System.Drawing.Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, this.dataGridView1.RowHeadersWidth - 4, e.RowBounds.Height);
                TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), this.dataGridView1.RowHeadersDefaultCellStyle.Font, bounds, this.dataGridView1.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
            }
            catch (Exception)
            {
            }
        }

        private void DataGridViewToExcel(DataGridView dgv)
        {
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.Filter = "Execl files (*.xls)|*.xls";
            dialog.FilterIndex = 0;
            dialog.RestoreDirectory = true;
            dialog.CreatePrompt = true;
            dialog.Title = "保存为Excel文件";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                Stream stream = dialog.OpenFile();
                StreamWriter writer = new StreamWriter(stream, Encoding.GetEncoding(0));
                string str = "";
                try
                {
                    for (int i = 0; i < (dgv.ColumnCount - 1); i++)
                    {
                        if (i > 0)
                        {
                            str = str + "\t";
                        }
                        str = str + dgv.Columns[i].HeaderText;
                    }
                    writer.WriteLine(str);
                    for (int j = 0; j < dgv.Rows.Count; j++)
                    {
                        string str2 = "";
                        for (int k = 0; k < dgv.Columns.Count; k++)
                        {
                            if (k > 0)
                            {
                                str2 = str2 + "\t";
                            }
                            if (dgv.Rows[j].Cells[k].Value == null)
                            {
                                str2 = str2 + "";
                            }
                            else
                            {
                                str2 = str2 + dgv.Rows[j].Cells[k].Value.ToString().Trim();
                            }
                        }
                        writer.WriteLine(str2);
                    }
                    writer.Close();
                    stream.Close();
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.ToString());
                }
                finally
                {
                    writer.Close();
                    stream.Close();
                }
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        public bool ExportDataGridview(DataGridView gridView, bool isShowExcle)
        {
            int num;
            if (gridView.Rows.Count == 0)
            {
                return false;
            }
            Excel.Application application = new ApplicationClass();

            application.Application.Workbooks.Add(true);
            application.Visible = isShowExcle;
            for (num = 0; num < (gridView.ColumnCount - 1); num++)
            {
                application.Cells[1, num + 1] = gridView.Columns[num].HeaderText;
            }
            for (num = 0; num < gridView.RowCount; num++)
            {
                for (int i = 0; i < (gridView.ColumnCount - 1); i++)
                {
                    if (gridView[i, num].ValueType == typeof(string))
                    {
                        application.Cells[num + 2, i + 1] = "'" + gridView[i, num].Value.ToString();
                    }
                    else
                    {
                        application.Cells[num + 2, i + 1] = gridView[i, num].Value.ToString();
                    }
                }
            }
            return true;
        }

        private void InitializeComponent()
        {
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            DataGridViewCellStyle style2 = new DataGridViewCellStyle();
            this.panel1 = new Panel();
            this.btn_DaoChu = new System.Windows.Forms.Button();
            this.btn_DaoRu = new System.Windows.Forms.Button();
            this.dataGridView1 = new DataGridView();
            this.Column5 = new DataGridViewTextBoxColumn();
            this.Column1 = new DataGridViewTextBoxColumn();
            this.Column2 = new DataGridViewTextBoxColumn();
            this.Column3 = new DataGridViewTextBoxColumn();
            this.Column4 = new DataGridViewTextBoxColumn();
            this.Column6 = new DataGridViewTextBoxColumn();
            this.comboBox1 = new ComboBox();
            this.label1 = new System.Windows.Forms.Label();
            this.openFileDialog1 = new OpenFileDialog();
            this.dataGridViewTextBoxColumn1 = new DataGridViewTextBoxColumn();
            this.dataGridViewTextBoxColumn2 = new DataGridViewTextBoxColumn();
            this.dataGridViewTextBoxColumn3 = new DataGridViewTextBoxColumn();
            this.dataGridViewTextBoxColumn4 = new DataGridViewTextBoxColumn();
            this.dataGridViewTextBoxColumn5 = new DataGridViewTextBoxColumn();
            this.dataGridViewTextBoxColumn6 = new DataGridViewTextBoxColumn();
            this.panel1.SuspendLayout();
            ((ISupportInitialize)this.dataGridView1).BeginInit();
            base.SuspendLayout();
            this.panel1.Controls.Add(this.btn_DaoChu);
            this.panel1.Controls.Add(this.btn_DaoRu);
            this.panel1.Controls.Add(this.dataGridView1);
            this.panel1.Controls.Add(this.comboBox1);
            this.panel1.Controls.Add(this.label1);
            this.panel1.Dock = DockStyle.Fill;
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new Size(0x37e, 0x206);
            this.panel1.TabIndex = 0;
            this.btn_DaoChu.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            this.btn_DaoChu.Location = new System.Drawing.Point(0x1e3, 0x1e3);
            this.btn_DaoChu.Name = "btn_DaoChu";
            this.btn_DaoChu.Size = new Size(0x5f, 0x17);
            this.btn_DaoChu.TabIndex = 7;
            this.btn_DaoChu.Text = "导出物料(&E)";
            this.btn_DaoChu.UseVisualStyleBackColor = true;
            this.btn_DaoChu.Click += new EventHandler(this.btn_DaoChu_Click);
            this.btn_DaoRu.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            this.btn_DaoRu.Location = new System.Drawing.Point(0x135, 0x1e3);
            this.btn_DaoRu.Name = "btn_DaoRu";
            this.btn_DaoRu.Size = new Size(0x6d, 0x17);
            this.btn_DaoRu.TabIndex = 3;
            this.btn_DaoRu.Text = "更新物料(&U)";
            this.btn_DaoRu.UseVisualStyleBackColor = true;
            this.btn_DaoRu.Click += new EventHandler(this.btn_DaoRu_Click);
            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            this.dataGridView1.BackgroundColor = SystemColors.ControlLightLight;
            this.dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Columns.AddRange(new DataGridViewColumn[] { this.Column5, this.Column1, this.Column2, this.Column3, this.Column4, this.Column6 });
            this.dataGridView1.Location = new System.Drawing.Point(11, 0x30);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.ReadOnly = true;
            this.dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders;
            this.dataGridView1.RowTemplate.Height = 0x17;
            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dataGridView1.Size = new Size(880, 0x1ad);
            this.dataGridView1.TabIndex = 2;
            this.dataGridView1.RowPostPaint += new DataGridViewRowPostPaintEventHandler(this.dataGridView1_RowPostPaint);
            this.Column5.FillWeight = 40f;
            this.Column5.HeaderText = "物料类别";
            this.Column5.Name = "Column5";
            this.Column5.ReadOnly = true;
            this.Column5.Visible = false;
            this.Column1.FillWeight = 70f;
            this.Column1.HeaderText = "物料名称";
            this.Column1.Name = "Column1";
            this.Column1.ReadOnly = true;
            this.Column1.SortMode = DataGridViewColumnSortMode.NotSortable;
            this.Column2.FillWeight = 40f;
            this.Column2.HeaderText = "单位";
            this.Column2.Name = "Column2";
            this.Column2.ReadOnly = true;
            this.Column2.SortMode = DataGridViewColumnSortMode.NotSortable;
            style.Format = "N2";
            style.NullValue = null;
            this.Column3.DefaultCellStyle = style;
            this.Column3.FillWeight = 40f;
            this.Column3.HeaderText = "单价";
            this.Column3.Name = "Column3";
            this.Column3.ReadOnly = true;
            this.Column3.SortMode = DataGridViewColumnSortMode.NotSortable;
            this.Column4.FillWeight = 250f;
            this.Column4.HeaderText = "工艺做法及说明";
            this.Column4.Name = "Column4";
            this.Column4.ReadOnly = true;
            this.Column4.SortMode = DataGridViewColumnSortMode.NotSortable;
            this.Column6.HeaderText = "id";
            this.Column6.Name = "Column6";
            this.Column6.ReadOnly = true;
            this.Column6.Visible = false;
            this.comboBox1.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
            this.comboBox1.FormattingEnabled = true;
            this.comboBox1.Location = new System.Drawing.Point(0x17a, 15);
            this.comboBox1.Name = "comboBox1";
            this.comboBox1.Size = new Size(0x84, 20);
            this.comboBox1.TabIndex = 1;
            this.comboBox1.SelectedIndexChanged += new EventHandler(this.comboBox1_SelectedIndexChanged);
            this.comboBox1.Click += new EventHandler(this.comboBox1_Click);
            this.label1.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top;
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(0x34, 15);
            this.label1.Name = "label1";
            this.label1.Size = new Size(0x29, 12);
            this.label1.TabIndex = 0;
            this.label1.Text = "label1";
            this.openFileDialog1.FileName = "openFileDialog1";
            this.dataGridViewTextBoxColumn1.FillWeight = 50f;
            this.dataGridViewTextBoxColumn1.HeaderText = "物料名称";
            this.dataGridViewTextBoxColumn1.Name = "dataGridViewTextBoxColumn1";
            this.dataGridViewTextBoxColumn1.ReadOnly = true;
            this.dataGridViewTextBoxColumn1.SortMode = DataGridViewColumnSortMode.NotSortable;
            this.dataGridViewTextBoxColumn1.Visible = false;
            this.dataGridViewTextBoxColumn1.Width = 160;
            this.dataGridViewTextBoxColumn2.FillWeight = 70f;
            this.dataGridViewTextBoxColumn2.HeaderText = "单位";
            this.dataGridViewTextBoxColumn2.Name = "dataGridViewTextBoxColumn2";
            this.dataGridViewTextBoxColumn2.ReadOnly = true;
            this.dataGridViewTextBoxColumn2.SortMode = DataGridViewColumnSortMode.NotSortable;
            this.dataGridViewTextBoxColumn2.Width = 70;
            this.dataGridViewTextBoxColumn3.FillWeight = 40f;
            this.dataGridViewTextBoxColumn3.HeaderText = "单价";
            this.dataGridViewTextBoxColumn3.Name = "dataGridViewTextBoxColumn3";
            this.dataGridViewTextBoxColumn3.ReadOnly = true;
            this.dataGridViewTextBoxColumn3.SortMode = DataGridViewColumnSortMode.NotSortable;
            this.dataGridViewTextBoxColumn3.Width = 80;
            style2.Format = "N2";
            style2.NullValue = null;
            this.dataGridViewTextBoxColumn4.DefaultCellStyle = style2;
            this.dataGridViewTextBoxColumn4.FillWeight = 40f;
            this.dataGridViewTextBoxColumn4.HeaderText = "工艺做法及说明";
            this.dataGridViewTextBoxColumn4.Name = "dataGridViewTextBoxColumn4";
            this.dataGridViewTextBoxColumn4.ReadOnly = true;
            this.dataGridViewTextBoxColumn4.SortMode = DataGridViewColumnSortMode.NotSortable;
            this.dataGridViewTextBoxColumn4.Width = 350;
            this.dataGridViewTextBoxColumn5.FillWeight = 160f;
            this.dataGridViewTextBoxColumn5.HeaderText = "工艺做法及说明";
            this.dataGridViewTextBoxColumn5.Name = "dataGridViewTextBoxColumn5";
            this.dataGridViewTextBoxColumn5.ReadOnly = true;
            this.dataGridViewTextBoxColumn5.SortMode = DataGridViewColumnSortMode.NotSortable;
            this.dataGridViewTextBoxColumn5.Width = 350;
            this.dataGridViewTextBoxColumn6.HeaderText = "id";
            this.dataGridViewTextBoxColumn6.Name = "dataGridViewTextBoxColumn6";
            this.dataGridViewTextBoxColumn6.ReadOnly = true;
            this.dataGridViewTextBoxColumn6.Visible = false;
            base.AutoScaleDimensions = new SizeF(6f, 12f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(0x37e, 0x206);
            base.Controls.Add(this.panel1);
            base.FormBorderStyle = FormBorderStyle.FixedSingle;
            base.Name = "Mater_Info";
            this.Text = "物料管理";
            base.Load += new EventHandler(this.Mater_Info_Load);
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            ((ISupportInitialize)this.dataGridView1).EndInit();
            base.ResumeLayout(false);
        }

        private void Mater_Info_Load(object sender, EventArgs e)
        {
            try
            {
            }
            catch (Exception)
            {
            }
        }
    }
}

